package epam.marchenko.library.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Table(name = "outcomes")
@NamedQueries({
	@NamedQuery(name = "OutcomeRecord.findAll", query = "SELECT r FROM OutcomeRecord r"),
	@NamedQuery(name = "OutcomeRecord.findAllForBook", query = "SELECT r FROM OutcomeRecord r WHERE r.book=:book"),
	@NamedQuery(name = "OutcomeRecord.findAllForUser", query = "SELECT r FROM OutcomeRecord r WHERE r.user=:user"),
	@NamedQuery(name = "OutcomeRecord.findAllForBookClass", query = "SELECT r FROM OutcomeRecord r WHERE r.book.bookClass=:bclass")
})
public class OutcomeRecord implements Serializable {
	
	private static final long serialVersionUID = 1L;
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", nullable = false)
	private int id;
	@ManyToOne()
	@JoinColumn(name = "user_id", nullable = false)
	private User user;
	@ManyToOne()
	@JoinColumn(name = "book_id", nullable = false)
	private Book book;
	@Temporal(value = TemporalType.DATE)
	@Column(name = "start_date")
	@DateTimeFormat(pattern = "dd/MM/yyyy")
	private Date startDate;
	@Temporal(value = TemporalType.DATE)
	@Column(name = "return_date")
	@DateTimeFormat(pattern = "dd/MM/yyyy")
	private Date returnDate;
	@OneToOne(mappedBy="outcome", cascade=CascadeType.REMOVE )
	private IncomeRecord incomeRecord;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public IncomeRecord getIncomeRecord() {
		return incomeRecord;
	}

	public void setIncomeRecord(IncomeRecord incomeRecord) {
		this.incomeRecord = incomeRecord;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getReturnDate() {
		return returnDate;
	}

	public void setReturnDate(Date returnDate) {
		this.returnDate = returnDate;
	}

	public boolean isActive() {
		return incomeRecord==null;
	}
	
	@Override
	public String toString() {
		return "OR " + id+" "+user+" "+book;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((book == null) ? 0 : book.hashCode());
		result = prime * result + id;
		result = prime * result
				+ ((returnDate == null) ? 0 : returnDate.hashCode());
		result = prime * result
				+ ((startDate == null) ? 0 : startDate.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OutcomeRecord other = (OutcomeRecord) obj;
		if (book == null) {
			if (other.book != null)
				return false;
		} else if (!book.equals(other.book))
			return false;
		if (id != other.id)
			return false;
		if (returnDate == null) {
			if (other.returnDate != null)
				return false;
		} else if (!returnDate.equals(other.returnDate))
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}
}
